package com.haiyangwang1.cainiao;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class AdminFrame extends JFrame {
    Employee[] e1 = new Employee[10];
    JFrame admin = new JFrame();
    private JLabel name, password;
    private JTextField txtInput;
    private JPasswordField pwd;
    private JButton btn1, btn2;
    private JPanel P1, P2, P3;
    private int index = 0;
    private int count = 0;
    private boolean isFlag = false;

    public AdminFrame() {
        initData();


        admin.setBounds(300, 200, 400, 300);
        //用户名和输入框
//        admin.setLayout(new GridLayout(3,2));
        admin.setLayout(new GridLayout(3, 2));
        P1 = new JPanel();
        name = new JLabel("用户名");
        txtInput = new JTextField(20);
        P1.add(name);
        P1.add(txtInput);
//        admin.add(P1);

        P2 = new JPanel();
        password = new JLabel("密码");
        pwd = new JPasswordField(20);
        P2.add(password);
        P2.add(pwd);

        P3 = new JPanel();
        btn1 = new JButton("确认");
        btn2 = new JButton("取消");
        btn2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        btn1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //判断用户名和密码是否正确
                String str = txtInput.getText();
                String passwords = new String(pwd.getPassword());
                count++;//点一次加一次
                for (int i = 0; i < index; i++) {

                    if (e1[i] != null) {

                        if (str.equals(e1[i].getName()) && passwords.equals(e1[i].getPassword())) {
                            isFlag = true;
                            String A = e1[i].getPosition();
                            switch (A){
                                case "管理员":
                                    ManagerFrame managerFrame = new ManagerFrame();
                                    admin.dispose();
                                    break;
                                case "员工":
                                    NormalFrame normalFrame = new NormalFrame();
                                    admin.dispose();
                                    break;
                            }
                            break;
                        }
                    }
                }
                if (count >= 3) {
                    JOptionPane.showMessageDialog(null, "非法用户，直接退出");
                    System.exit(0);
                } else if (count < 3 && !isFlag) {
                    JOptionPane.showMessageDialog(null, "你还有" + (3 - count) + "次机会");

                }

//                            if (e1[index].getPosition().equals("管理员")) {
//                                ManagerFrame managerFrame = new ManagerFrame();
//                                admin.dispose();
//                            } else if (e1[index].getPosition().equals("员工")) {
//                                NormalFrame normalFrame = new NormalFrame();
//                                admin.dispose();
//                        }

//                        System.out.println("进来了");

//                        }else if (i == index - 1){//当初始数据里面没有找到相同的密码和姓名的时候
//                            JOptionPane.showMessageDialog(null,"用户名或密码错误");
//                            break;
//                        }
//

            }


        });
        P3.add(btn1);
        P3.add(btn2);
        admin.add(P1, BorderLayout.CENTER);
        admin.add(P2, BorderLayout.CENTER);
        admin.add(P3, BorderLayout.SOUTH);
        admin.setVisible(true);
        admin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }


    public void initData() {

        e1[0] = new Employee("jack", "666", "管理员");
        index++;//每增加一条数据加一次  遍历数组就遍历到底有多少个数据
        e1[1] = new Employee("lily", "555", "员工");
        index++;
    }
}
